﻿using jutehui.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace GetData
{
    public class HotProductsCalss
    {
        /// <summary>
        /// 通过网页html获取商品信息并保存到数据库中
        /// </summary>
        /// <param name="url"></param>
        /// <param name="shoppingType">海淘2 精选1</param>
        /// <returns></returns>
        public string GetProductsToDataBase(string url)
        {
            try
            {
                var htmlText = CustomMethod.GetHtmlByHttpHelper(url);
                string msg = "";
                var pList = GetProductByRegex(htmlText);
                if (pList == null)
                {
                    CustomMethod.WriteLog("小时榜没有抓取到数据");
                    return "";
                }
                DataTable dt = CustomMethod.ListToDataTable<Product>(pList, "Product", ref msg);
                if (dt == null || dt.Rows.Count < 1)
                {
                    CustomMethod.WriteLog("小时榜：" + msg);
                    return "";
                }
                jutehui.BLL.Product bll = new jutehui.BLL.Product();
                if (bll.SaveHourTopData("Product", dt, out msg))
                {
                    // string errLog = "";
                    //if (!bll.SaveModelListByCache(ref errLog))
                    //{
                    //    CustomMethod.WriteLog(errLog);
                    //} 
                    return dt.Rows.Count.ToString(); ;
                }
                else
                {
                    msg = "小时风云榜：GetProductsToDataBase" + msg;
                    CustomMethod.WriteLog(msg);
                    return "";

                }
            }
            catch (Exception ex)
            {
            
                CustomMethod.WriteLog("小时榜：" + ex.Message.ToString());
                return "";
            }
        }
        /// <summary>
        /// 根据正则解析商品信息
        /// </summary>
        /// <param name="htmlText"></param> 
        /// <returns></returns>
        public List<Product> GetProductByRegex(string htmlText)
        {
            List<Product> pList = new List<Product>();
            string productRegex = @"<div class=""hourrankitem"">[\s\S]*?</div>\s+</div>\s+</div>";

            var list = CustomMethod.GetInfoByRegex(productRegex, htmlText, 0);
            if (list == null || list.Count < 1)
            {
                CustomMethod.WriteLog("小时风云榜：GetProductByRegex 网站没有解析出风云榜信息。");
                return null;
            }
            for (int index = 0; index < list.Count; index++)
            {
                Product product = new Product();
                try
                {
                    var herfList = CustomMethod.GetInfoFroma(list[index]);
                    if (herfList == null || herfList.Count < 1)
                    {
                        continue;
                    }
                    string imgUrl = CustomMethod.GetImgUrl(list[index], "小时风云榜");
                    if (imgUrl != "")
                    {
                        product.ImgUrl = imgUrl.Split('?').ToList()[0];
                    }
                    string altRegex = @"alt=[""|']([^""|']*)[""|']";
                    var titlList = CustomMethod.GetInfoByRegex(altRegex, list[index], 1);
                    if (titlList == null || titlList.Count < 1)
                    {
                        continue;
                    }
                    product.Pname = titlList[0];
                    string url = "http://guangdiu.com/go.php?" + herfList[0].Split('?').ToList()[1];
                    string productUrl = "";
                    product.Plink = CustomMethod.GetEvalUrl(url, ref productUrl, CustomMethod.typeOfWeb.HotProduct);
                    product.Memo = productUrl;
                    if (product.Plink == "")
                    {
                        if (productUrl == "")
                        {
                            CustomMethod.WriteLog("小时风云榜:未能获取商品链接");
                        }
                        continue;
                    }
                    string detailUrl = "http://guangdiu.com" + herfList[0].Substring(1, herfList[0].Length - 1);

                    string detailHtml = CustomMethod.GetHtmlByHttpHelper(detailUrl);
                    string desRegex = @"<div class=""dabstract"" id=""dabstract"">[\s\S]*?<div class=""dtomobilearea dtomplain"">";
                    var desList = CustomMethod.GetInfoByRegex(desRegex, detailHtml, 0);
                    //详细内容图片信息
                    string srcRegexn = @"src=[""|']([^""|']*)[""|']";
                    var srcImgn = CustomMethod.GetInfoByRegex(srcRegexn, desList[0], 1);
                    if (srcImgn != null && srcImgn.Count > 0)
                    {
                        if (srcImgn.Count == 1)
                        {
                            product.ImgUrl2 = srcImgn[0];
                        }
                        else if (srcImgn.Count == 2)
                        {
                            product.ImgUrl2 = srcImgn[0];
                            product.ImgUrl3 = srcImgn[1];
                        }
                        else if (srcImgn.Count > 2)
                        {
                            product.ImgUrl2 = srcImgn[0];
                            product.ImgUrl3 = srcImgn[1];
                            product.ImgUrl4 = srcImgn[2];
                        }

                    }
                    //描述信息 
                    if (!desList[0].Contains("<p>"))
                    {
                        string des = Regex.Replace(desList[0], @"<(.[^>]*)>", "");
                        product.Description = des;
                    }
                    else
                    {
                        string desContentRegex = @"<p>[\s\S]*?</p>";
                        string desCont = "";
                        var desContentList = CustomMethod.GetInfoByRegex(desContentRegex, desList[0], 0);
                        for (int count = 0; count < desContentList.Count; count++)
                        {
                            desCont += "|||" + Regex.Replace(desContentList[count], @"<(.[^>]*)>", "");
                        }
                        product.Description = desCont.Replace("直达链接", "");
                        product.Description = product.Description == "" ? product.Pname : product.Description;
                    }
                    //来源hourrankfromsite
                    string sourceRegex = @"<span class=""hourrankmalltext"">([^<]+)</span>";
                    var sourceList = CustomMethod.GetInfoByRegex(sourceRegex, list[index], 1);
                    if (sourceList != null && sourceList.Count > 0)
                    {
                        product.Source = sourceList[0];
                    }
                    product.Price = "";
                    //前一小时的风云榜
                    product.TypeId = 0;
                    product.UpdateTime = DateTime.Now.AddHours(-1);
                    product.UpdateTimeStr = Convert.ToDateTime(product.UpdateTime).ToString("yyyy-MM-dd HH:mm:ss");
                    product.Pname = product.Pname.Replace("&#x2F;", "/").Replace("#039;", "'").Replace("&amp;", "&").Replace("&#x27;", "'").Replace("&quot;", "\""); ; ;
                    product.Description.Replace("&#x2F;", "/").Replace("#039;", "'");
                }
                catch (Exception ex)
                {
                    CustomMethod.WriteLog("小时风云榜：" + ex.Message.ToString() + DateTime.Now);

                }
                pList.Add(product);
            }
            var newList = CustomMethod.GetDistinctProdcuts(pList); 
            return newList;
            
        }
    }
}
